import modelAbstract from "./modelAbstract";
import { images } from "../service/image";
import { directionEnum } from '../enum/directionEnum';
import untils from "../untils";
import config from "../config";
import tank from "../canvas/tank";
// 导入库
import _ from 'lodash';

// 草地模型子类
export default class tankModel extends modelAbstract implements IModel{
  public ctx: ICanvas = tank;
  name: string = 'tank';
  render(): void {
    this.move();
    if (Math.floor(Math.random() * 20) == 1) {
      this.direction = directionEnum.bottom;
    }
    // if (_.random(10) == 1) this.direction = directionEnum.bottom;
  };
  public move() {
    while (true) {
      let x = this.posX;
      let y = this.posY;
      switch (this.direction) {
        case directionEnum.top:
          y --;
          break;
        case directionEnum.bottom:
          y ++;
          break;
        case directionEnum.left:
          x --;
          break;
        case directionEnum.right:
          x ++;
          break;
      };
      if (untils.isCanvasTouch(x, y, this.width, this.height)
        || untils.isModelTouch(x, y, this.width, this.height)) {
        this.randomDirection();
      } else {
        this.posY = y;
        this.posX = x;
        break;
      }
    };
    super.draw();
  };
  image() {
    return this.randomImg()!;
  };
  // 获取随机方向的贴图
  randomImg() {
    // 通过lodash库将字符串首字母转化成大写 下载lodash @types/lodash
    let direction = this.name + _.upperFirst(this.direction);
    return images.get(direction as keyof typeof config.images);
  };

};